package cn.lwj.controller;


import cn.lwj.common.BaseContext;
import cn.lwj.common.R;
import cn.lwj.pojo.AddressBook;
import cn.lwj.service.AddressBookService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import kotlin.jvm.JvmDefaultWithCompatibility;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.net.httpserver.HttpsServerImpl;

import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.rmi.server.RemoteRef;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 获取地址列表
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(HttpSession session){
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        Object userId = session.getAttribute("user");
        lqw.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(lqw);
        return R.success(list);
    }

    /**
     * 修改默认收货地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<String> updateDefaultAddress(@RequestBody AddressBook addressBook){
        System.out.println("id ==== " + addressBook.getId());
        addressBookService.updateDefaultAdress(addressBook.getId());
        return R.success("修改成功！");
    }

    /**
     * 新增收货地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook, HttpSession session){
        long userId = (long) session.getAttribute("user");
//        System.out.println(userId);
        if (addressBook != null) {
            addressBook.setUserId(userId);
            addressBookService.save(addressBook);
        }
        return R.success("新增收货地址成功");
    }


    /**
     * 通过id获取地址信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getAddressBookById(@PathVariable long id){

        AddressBook addressBook = addressBookService.getById(id);

        if (addressBook != null) {
            return R.success(addressBook);
        }else {
            return R.error("回显地址数据失败");
        }
    }


    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
//        System.out.println(addressBook.toString());
        if( addressBook != null){
            addressBookService.updateById(addressBook);
        }
        return R.success("修改成功");
    }

    @DeleteMapping
    public R<String> delete(Long ids){
        addressBookService.removeById(ids);
        return R.success("删除成功");
    }

    /**
     * 获取当前用户默认收货地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefaultAddress(){
        long userid = BaseContext.getCurrentId();

        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();

        lqw.eq(AddressBook::getUserId,userid);

        lqw.eq(AddressBook::getIsDefault,1);

        AddressBook addressBook = addressBookService.getOne(lqw);

        return R.success(addressBook);
    }


}
